package com.fuliangyun.weather.utils;

import android.util.Log;
import com.fuliangyun.weather.db.City;
import com.fuliangyun.weather.db.County;
import com.fuliangyun.weather.db.Province;
import com.fuliangyun.weather.gson.Weather;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Utility {

    /**
     * 解析和处理服务器返回的省级数据
     * @param response
     * @return
     */
    public static boolean handleProvinceResponse(String response){
        if (!"".equals(response)){
            try {
                Log.i("返回数据：", response);
                JSONArray allProvinces = new JSONArray(response);
                for (int i = 0;i<allProvinces.length();i++){
                    JSONObject j = (JSONObject) allProvinces.get(i);
                    Province p = new Province();
                    p.setProvinceName(j.get("name").toString());
                    p.setProvinceCode(Integer.parseInt(j.get("id").toString()));
                    p.save();
                }
                return true;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 解析和处理服务器返回的市级数据
     * @param response
     * @return
     */
    public static boolean handleCityResponse(String response, int provinceId){
        if (!"".equals(response)){
            try {
                JSONArray allCities = new JSONArray(response);
                for (int i = 0;i<allCities.length();i++){
                    JSONObject j = (JSONObject) allCities.get(i);
                    City c = new City();
                    c.setCityName(j.get("name").toString());
                    c.setCityCode(Integer.parseInt(j.get("id").toString()));
                    c.setProvinceId(provinceId);
                    c.save();
                }
                return true;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 解析和处理服务器返回的市级数据
     * @param response
     * @return
     */
    public static boolean handleCountyResponse(String response, int cityId){
        if (!"".equals(response)){
            try {
                JSONArray allCounties = new JSONArray(response);
                for (int i = 0;i<allCounties.length();i++){
                    JSONObject j = (JSONObject) allCounties.get(i);
                    County c = new County();
                    c.setCountyName(j.get("name").toString());
                    c.setWeatherId(j.get("weather_id").toString());
                    c.setCityId(cityId);
                    c.save();
                }
                return true;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 解析天气信息
     * @param response
     * @return
     */
    public static Weather handleWeatherResponse(String response){
        if (!"".equals(response)){
            try {
                JSONObject j = new JSONObject(response);

                JSONArray ja = j.getJSONArray("HeWeather6");

                String c = ja.getJSONObject(0).toString();
                return new Gson().fromJson(c, Weather.class);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}
